/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.utils;

import com.flexengine.logger.Logger;
import com.flexengine.view.Sprite;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class SpriteManager {
    
    private final HashMap<String,Sprite> spriteMap;
    
    private SpriteManager() {
        this.spriteMap = new HashMap<>();
    }
    
    private Sprite loadSprite(File folder) {
        
        return null;
    }
    
    private boolean load(String folder) {
        File fFolder = new File(ResourceManager.getLocale(folder));
        if( !fFolder.isDirectory() ) {
            return false;
        }
        // ---------------------------------------------------------------------
        File folders[] = fFolder.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return dir.isDirectory();
            }
        });
        if( folders==null || folders.length<=0 ) {
            return false;
        }
        // ---------------------------------------------------------------------
        
        int spritesLoaded = 0;
        for (File folder1 : folders) {
            Sprite spr = loadSprite(folder1);
            if( spr != null ) {
                Logger.logMessage(getClass().getSimpleName()+" - loadSprites(): sprite loaded = "+spr);
                spriteMap.put(spr.getName(), spr);
                spritesLoaded++;
            }
        }
        Logger.logMessage(getClass().getSimpleName()+" - loadSprites(): Total sprites loaded = "+spritesLoaded);
        return true;
    }
    
    /**
     * Load all sprites in recursive mode contains in ./resources/sprites/ and subfolders...
     * @return 
     */
    public boolean loadSprites() {
        Logger.logMessage("SpriteManager - loadSprites(): Loading sprites...");
        return load("sprites");
    }
    /**
     * Load all textures in recursive mode conatis in ./resources/folder and subfolders... 
     * @param folder
     * @return true|false
     */
    public boolean loadSprites(String folder) {
        Logger.logMessage("SpriteManager - loadSprites(): Loading sprites...");
        return load(folder);
    }
    
    /**
     * <p>Load a sprite. If the sprite is don't loaded, then will load.</p>
     * <p>PS: baseTexture used to = (textureName)001.png<br/>
     * (textureName)002.png ...
     * <p>
     * <p>
     * @param path        - The path for the textures... (Ex.: /textures/grass/)
     * @param animatedBy  - Sprite.ANIMETED_BY_FRAMES || Sprite.ANIMETED_BY_TIME
     * @param textureName - the default texture name (Ex.: grass )
     * @param numTextures - number of textures to load...
     * @param time[]      - Array with time for each frame...
     * @return sprite
     */
    public Sprite load(String path, int animatedBy, String textureName, int numTextures, long time[]) {
        return null;
    }

    public int size() {
        return spriteMap.size();
    }

    public boolean isEmpty() {
        return spriteMap.isEmpty();
    }

    public boolean containsKey(String key) {
        return spriteMap.containsKey(key);
    }

    public boolean containsValue(Sprite value) {
        return spriteMap.containsValue(value);
    }

    public Sprite get(String key) {
        return spriteMap.get(key);
    }
    public Sprite get(String key, String path, String baseTexture, long time) {
        return get(key, path, Sprite.ANIMETED_BY_FRAMES, baseTexture, 1, new long[]{time});
    }
    public Sprite get(String key, String path, String baseTexture, int numTextures, long time[]) {
        return get(key, path, Sprite.ANIMETED_BY_FRAMES, baseTexture, numTextures, time);
    }
    /**
     * Get a sprite from the container. If the sprite is don't loaded, then will load.<br/>
     * PS: baseTexture used to = (textureName)001.png<br/>
     * (textureName)002.png ...<br/>
     * @param keyName     Key name for the sprite
     * @param basePath    The path for the textures... (Ex.: /sprites/grass/)
     * @param animatedBy  Sprite.ANIMETED_BY_FRAMES || Sprite.ANIMETED_BY_TIME
     * @param baseTexture texture name (Ex.: grass )
     * @param numTextures number of textures to load...
     * @param time[]      time in frames to each texture...
     * @return sprite
     */
    public Sprite get(String keyName, String basePath, int animatedBy, String baseTexture, int numTextures, long time[]) {
        Sprite sprite = spriteMap.get(keyName);
        if( sprite == null ) {
            sprite = load(basePath, animatedBy, baseTexture, numTextures, time);
            spriteMap.put(keyName, sprite);
        }
        return sprite;
    }

    public Sprite put(String key, Sprite sprite) {
        return spriteMap.put(key, sprite);
    }

    public Sprite remove(String key) {
        return spriteMap.remove(key);
    }

    public void clear() {
        spriteMap.clear();
    }

    public Collection<Sprite> values() {
        return spriteMap.values();
    }

    public Set<String> getKeySet()  {
        return spriteMap.keySet();
    }
    
    private static SpriteManager instance;
    public static SpriteManager getInstance() {
        if( instance == null ) {
            instance = new SpriteManager();
        }
        return instance;
    }
    
}
